﻿namespace PlanckSys.Jira.RssJiraRedirector
{

    #region Imports
    using EventLog = System.Diagnostics.EventLog;
    using Exception = System.Exception;
    #endregion

    static class Program
    {



        /// <summary>
        /// The main entry point for the service...
        /// It will run the executable as a service
        /// unless /c is specified in which case it will run as a console app.
        /// </summary>
        static int Main(string[] args)
        {
            int retCode = 0;
            try
            {


                if (args.Length > 1)
                {
                    PrintUsage();
                    retCode = 1;

                }
                else if (args.Length == 1)
                {
                    if ((args[0] != null) &&
                        (args[0].Equals("/c") || args[0].Equals("-c")
                        || args[0].Equals("/C") || args[0].Equals("-C")
                        )
                       )
                    { //run service as console app
                        RssJiraRedirectorService.RunConsole();
                    }
                    else
                    {
                        PrintUsage();
                        retCode = 1;
                    }
                }
                else
                { //run normal service call
                    RssJiraRedirectorService.RunService();
                }
            }
            catch (Exception ex)
            {

                //To create the source, you need permission to read all event logs to make sure
                //that the new source name is unique.  Inaccessible logs: Security.
                //If you use the sourceName of the Service you added the event log to then you dont have to create
                string SourceName = "RssJiraRedirectorService";
                string logName = "Application";
                try
                {
                    if (!System.Diagnostics.EventLog.SourceExists(SourceName))
                    {
                        EventLog.CreateEventSource(SourceName, logName);
                    }
                }
                catch (Exception eIgnore)
                {
                    try
                    {
                        //we are not looking in security logs so ignore
                        EventLog.CreateEventSource(SourceName, logName);
                    }
                    catch (Exception eIgnore1)
                    {
                        throw;
                    }
                }
                EventLog eventLog = new EventLog();
                eventLog.Source = SourceName;

                throw;

            }

            return retCode;

        }

        private static void PrintUsage()
        {
            System.Console.WriteLine("PlanckSys.Jira.RssJiraRedirector.exe /C");
        }
    }
}
